<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
                <el-form-item label="主键ID" prop="id">
                      <el-input
                          v-model="queryParams.id"
                          placeholder="主键ID"
                          clearable
                          @keyup.enter="handleQuery()"
                      />
                </el-form-item>
                <el-form-item label="日期" prop="date">
                      <el-date-picker
                          class="!w-[240px]"
                          v-model="queryParams.date"
                          type="daterange"
                          range-separator="~"
                          start-placeholder="开始时间"
                          end-placeholder="结束时间"
                          value-format="YYYY-MM-DD"
                      />
                </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <el-button
            v-hasPerm="['system:bigcap:add']"
            type="success"
            @click="handleOpenDialog()"
        >
          <template #icon><Plus /></template>
          新增
        </el-button>
        <el-button
            v-hasPerm="['system:bigcap:delete']"
            type="danger"
            :disabled="removeIds.length === 0"
            @click="handleDelete()"
        >
          <template #icon><Delete /></template>
          删除
        </el-button>
      </div>

      <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="pageData"
          highlight-current-row
          border
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
                    <el-table-column
                        key="id"
                        label="主键ID"
                        prop="id"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="date"
                        label="日期"
                        prop="date"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="trafficIndex"
                        label="流量指数"
                        prop="trafficIndex"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="trafficIndexYoy"
                        label="流量指数同比(年)"
                        prop="trafficIndexYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="visitorIndex"
                        label="访客指数"
                        prop="visitorIndex"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="visitorIndexYoy"
                        label="访客指数同比(年)"
                        prop="visitorIndexYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="followers"
                        label="关注人数"
                        prop="followers"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="followersYoy"
                        label="关注人数同比(年)"
                        prop="followersYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="addToCart"
                        label="加购人数"
                        prop="addToCart"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="addToCartYoy"
                        label="加购人数同比(年)"
                        prop="addToCartYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="categorySearchIndex"
                        label="类目搜索指数"
                        prop="categorySearchIndex"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="categorySearchIndexYoy"
                        label="类目搜索指数同比(年)"
                        prop="categorySearchIndexYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="categoryClickIndex"
                        label="类目点击指数"
                        prop="categoryClickIndex"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="categoryClickIndexYoy"
                        label="类目点击指数同比(年)"
                        prop="categoryClickIndexYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="clickRate"
                        label="搜索点击率"
                        prop="clickRate"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="clickRateYoy"
                        label="搜索点击率同比(年)"
                        prop="clickRateYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="transactionAmountIndex"
                        label="成交金额指数"
                        prop="transactionAmountIndex"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="transactionAmountIndexYoy"
                        label="成交金额指数同比(年)"
                        prop="transactionAmountIndexYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="transactionVolumeIndex"
                        label="成交件数指数"
                        prop="transactionVolumeIndex"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="transactionVolumeIndexYoy"
                        label="成交件数指数同比(年)"
                        prop="transactionVolumeIndexYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="conversionRate"
                        label="成交转化率"
                        prop="conversionRate"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="conversionRateYoy"
                        label="成交转化率同比(年)"
                        prop="conversionRateYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="customerPrice"
                        label="成交客单价"
                        prop="customerPrice"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="customerPriceYoy"
                        label="成交客单价同比(年)"
                        prop="customerPriceYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="brandCount"
                        label="品牌数"
                        prop="brandCount"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="brandCountYoy"
                        label="品牌数同比(年)"
                        prop="brandCountYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="visitedBrandCount"
                        label="被访问品牌数"
                        prop="visitedBrandCount"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="visitedBrandCountYoy"
                        label="被访问品牌数同比(年)"
                        prop="visitedBrandCountYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="activeBrandCount"
                        label="动销品牌数"
                        prop="activeBrandCount"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="activeBrandCountYoy"
                        label="动销品牌数同比(年)"
                        prop="activeBrandCountYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="storeCount"
                        label="店铺数"
                        prop="storeCount"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="storeCountYoy"
                        label="店铺数同比(年)"
                        prop="storeCountYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="visitedStoreCount"
                        label="被访问店铺数"
                        prop="visitedStoreCount"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="visitedStoreCountYoy"
                        label="被访问店铺数同比(年)"
                        prop="visitedStoreCountYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="activeStoreCount"
                        label="动销店铺数"
                        prop="activeStoreCount"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="activeStoreCountYoy"
                        label="动销店铺数同比(年)"
                        prop="activeStoreCountYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="visitedProductCount"
                        label="被访问商品数"
                        prop="visitedProductCount"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="visitedProductCountYoy"
                        label="被访问商品数同比(年)"
                        prop="visitedProductCountYoy"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="activeProductCount"
                        label="动销商品数"
                        prop="activeProductCount"
                        min-width="150"
                        align="center"
                    />
                    <el-table-column
                        key="activeProductCountYoy"
                        label="动销商品数同比(年)"
                        prop="activeProductCountYoy"
                        min-width="150"
                        align="center"
                    />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
                v-hasPerm="['system:bigcap:edit']"
                type="primary"
                size="small"
                link
                @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon><Edit /></template>
              编辑
            </el-button>
            <el-button
                v-hasPerm="['system:bigcap:delete']"
                type="danger"
                size="small"
                link
                @click="handleDelete(scope.row.id)"
            >
              <template #icon><Delete /></template>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleQuery()"
      />
    </el-card>

    <!-- 大盘底表单弹窗 -->
    <el-dialog
        v-model="dialog.visible"
        :title="dialog.title"
        width="500px"
        @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
                <el-form-item label="主键ID" prop="id">
                      <el-input
                          v-model="formData.id"
                          placeholder="主键ID"
                      />
                </el-form-item>

                <el-form-item label="日期" prop="date">
                      <el-date-picker
                          class="!w-[240px]"
                          v-model="formData.date"
                          type="date"
                          placeholder="日期"
                          value-format="YYYY-MM-DD"
                      />
                </el-form-item>

                <el-form-item label="流量指数" prop="trafficIndex">
                      <el-input
                          v-model="formData.trafficIndex"
                          placeholder="流量指数"
                      />
                </el-form-item>

                <el-form-item label="流量指数同比(年)" prop="trafficIndexYoy">
                      <el-input
                          v-model="formData.trafficIndexYoy"
                          placeholder="流量指数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="访客指数" prop="visitorIndex">
                      <el-input
                          v-model="formData.visitorIndex"
                          placeholder="访客指数"
                      />
                </el-form-item>

                <el-form-item label="访客指数同比(年)" prop="visitorIndexYoy">
                      <el-input
                          v-model="formData.visitorIndexYoy"
                          placeholder="访客指数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="关注人数" prop="followers">
                      <el-input
                          v-model="formData.followers"
                          placeholder="关注人数"
                      />
                </el-form-item>

                <el-form-item label="关注人数同比(年)" prop="followersYoy">
                      <el-input
                          v-model="formData.followersYoy"
                          placeholder="关注人数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="加购人数" prop="addToCart">
                      <el-input
                          v-model="formData.addToCart"
                          placeholder="加购人数"
                      />
                </el-form-item>

                <el-form-item label="加购人数同比(年)" prop="addToCartYoy">
                      <el-input
                          v-model="formData.addToCartYoy"
                          placeholder="加购人数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="类目搜索指数" prop="categorySearchIndex">
                      <el-input
                          v-model="formData.categorySearchIndex"
                          placeholder="类目搜索指数"
                      />
                </el-form-item>

                <el-form-item label="类目搜索指数同比(年)" prop="categorySearchIndexYoy">
                      <el-input
                          v-model="formData.categorySearchIndexYoy"
                          placeholder="类目搜索指数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="类目点击指数" prop="categoryClickIndex">
                      <el-input
                          v-model="formData.categoryClickIndex"
                          placeholder="类目点击指数"
                      />
                </el-form-item>

                <el-form-item label="类目点击指数同比(年)" prop="categoryClickIndexYoy">
                      <el-input
                          v-model="formData.categoryClickIndexYoy"
                          placeholder="类目点击指数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="搜索点击率" prop="clickRate">
                      <el-input
                          v-model="formData.clickRate"
                          placeholder="搜索点击率"
                      />
                </el-form-item>

                <el-form-item label="搜索点击率同比(年)" prop="clickRateYoy">
                      <el-input
                          v-model="formData.clickRateYoy"
                          placeholder="搜索点击率同比(年)"
                      />
                </el-form-item>

                <el-form-item label="成交金额指数" prop="transactionAmountIndex">
                      <el-input
                          v-model="formData.transactionAmountIndex"
                          placeholder="成交金额指数"
                      />
                </el-form-item>

                <el-form-item label="成交金额指数同比(年)" prop="transactionAmountIndexYoy">
                      <el-input
                          v-model="formData.transactionAmountIndexYoy"
                          placeholder="成交金额指数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="成交件数指数" prop="transactionVolumeIndex">
                      <el-input
                          v-model="formData.transactionVolumeIndex"
                          placeholder="成交件数指数"
                      />
                </el-form-item>

                <el-form-item label="成交件数指数同比(年)" prop="transactionVolumeIndexYoy">
                      <el-input
                          v-model="formData.transactionVolumeIndexYoy"
                          placeholder="成交件数指数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="成交转化率" prop="conversionRate">
                      <el-input
                          v-model="formData.conversionRate"
                          placeholder="成交转化率"
                      />
                </el-form-item>

                <el-form-item label="成交转化率同比(年)" prop="conversionRateYoy">
                      <el-input
                          v-model="formData.conversionRateYoy"
                          placeholder="成交转化率同比(年)"
                      />
                </el-form-item>

                <el-form-item label="成交客单价" prop="customerPrice">
                      <el-input
                          v-model="formData.customerPrice"
                          placeholder="成交客单价"
                      />
                </el-form-item>

                <el-form-item label="成交客单价同比(年)" prop="customerPriceYoy">
                      <el-input
                          v-model="formData.customerPriceYoy"
                          placeholder="成交客单价同比(年)"
                      />
                </el-form-item>

                <el-form-item label="品牌数" prop="brandCount">
                      <el-input
                          v-model="formData.brandCount"
                          placeholder="品牌数"
                      />
                </el-form-item>

                <el-form-item label="品牌数同比(年)" prop="brandCountYoy">
                      <el-input
                          v-model="formData.brandCountYoy"
                          placeholder="品牌数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="被访问品牌数" prop="visitedBrandCount">
                      <el-input
                          v-model="formData.visitedBrandCount"
                          placeholder="被访问品牌数"
                      />
                </el-form-item>

                <el-form-item label="被访问品牌数同比(年)" prop="visitedBrandCountYoy">
                      <el-input
                          v-model="formData.visitedBrandCountYoy"
                          placeholder="被访问品牌数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="动销品牌数" prop="activeBrandCount">
                      <el-input
                          v-model="formData.activeBrandCount"
                          placeholder="动销品牌数"
                      />
                </el-form-item>

                <el-form-item label="动销品牌数同比(年)" prop="activeBrandCountYoy">
                      <el-input
                          v-model="formData.activeBrandCountYoy"
                          placeholder="动销品牌数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="店铺数" prop="storeCount">
                      <el-input
                          v-model="formData.storeCount"
                          placeholder="店铺数"
                      />
                </el-form-item>

                <el-form-item label="店铺数同比(年)" prop="storeCountYoy">
                      <el-input
                          v-model="formData.storeCountYoy"
                          placeholder="店铺数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="被访问店铺数" prop="visitedStoreCount">
                      <el-input
                          v-model="formData.visitedStoreCount"
                          placeholder="被访问店铺数"
                      />
                </el-form-item>

                <el-form-item label="被访问店铺数同比(年)" prop="visitedStoreCountYoy">
                      <el-input
                          v-model="formData.visitedStoreCountYoy"
                          placeholder="被访问店铺数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="动销店铺数" prop="activeStoreCount">
                      <el-input
                          v-model="formData.activeStoreCount"
                          placeholder="动销店铺数"
                      />
                </el-form-item>

                <el-form-item label="动销店铺数同比(年)" prop="activeStoreCountYoy">
                      <el-input
                          v-model="formData.activeStoreCountYoy"
                          placeholder="动销店铺数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="被访问商品数" prop="visitedProductCount">
                      <el-input
                          v-model="formData.visitedProductCount"
                          placeholder="被访问商品数"
                      />
                </el-form-item>

                <el-form-item label="被访问商品数同比(年)" prop="visitedProductCountYoy">
                      <el-input
                          v-model="formData.visitedProductCountYoy"
                          placeholder="被访问商品数同比(年)"
                      />
                </el-form-item>

                <el-form-item label="动销商品数" prop="activeProductCount">
                      <el-input
                          v-model="formData.activeProductCount"
                          placeholder="动销商品数"
                      />
                </el-form-item>

                <el-form-item label="动销商品数同比(年)" prop="activeProductCountYoy">
                      <el-input
                          v-model="formData.activeProductCountYoy"
                          placeholder="动销商品数同比(年)"
                      />
                </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
  defineOptions({
    name: "Bigcap",
    inheritAttrs: false,
  });

  import BigcapAPI, { BigcapPageVO, BigcapForm, BigcapPageQuery } from "@/api/system/bigcap";

  const queryFormRef = ref();
  const dataFormRef = ref();

  const loading = ref(false);
  const removeIds = ref<number[]>([]);
  const total = ref(0);

  const queryParams = reactive<BigcapPageQuery>({
    pageNum: 1,
    pageSize: 10,
  });

  // 大盘底表格数据
  const pageData = ref<BigcapPageVO[]>([]);

  // 弹窗
  const dialog = reactive({
    title: "",
    visible: false,
  });

  // 大盘底表单数据
  const formData = reactive<BigcapForm>({});

  // 大盘底表单校验规则
  const rules = reactive({
                      id: [{ required: true, message: "请输入主键ID", trigger: "blur" }],
                      date: [{ required: true, message: "请输入日期", trigger: "blur" }],
  });

  /** 查询大盘底 */
  function handleQuery() {
    loading.value = true;
          BigcapAPI.getPage(queryParams)
        .then((data) => {
          pageData.value = data.list;
          total.value = data.total;
        })
        .finally(() => {
          loading.value = false;
        });
  }

  /** 重置大盘底查询 */
  function handleResetQuery() {
    queryFormRef.value!.resetFields();
    queryParams.pageNum = 1;
    handleQuery();
  }

  /** 行复选框选中记录选中ID集合 */
  function handleSelectionChange(selection: any) {
    removeIds.value = selection.map((item: any) => item.id);
  }

  /** 打开大盘底弹窗 */
  function handleOpenDialog(id?: number) {
    dialog.visible = true;
    if (id) {
      dialog.title = "修改大盘底";
            BigcapAPI.getFormData(id).then((data) => {
        Object.assign(formData, data);
      });
    } else {
      dialog.title = "新增大盘底";
    }
  }

  /** 提交大盘底表单 */
  function handleSubmit() {
    dataFormRef.value.validate((valid: any) => {
      if (valid) {
        loading.value = true;
        const id = formData.id;
        if (id) {
                BigcapAPI.update(id, formData)
              .then(() => {
                ElMessage.success("修改成功");
                handleCloseDialog();
                handleResetQuery();
              })
              .finally(() => (loading.value = false));
        } else {
                BigcapAPI.add(formData)
              .then(() => {
                ElMessage.success("新增成功");
                handleCloseDialog();
                handleResetQuery();
              })
              .finally(() => (loading.value = false));
        }
      }
    });
  }

  /** 关闭大盘底弹窗 */
  function handleCloseDialog() {
    dialog.visible = false;
    dataFormRef.value.resetFields();
    dataFormRef.value.clearValidate();
    formData.id = undefined;
  }

  /** 删除大盘底 */
  function handleDelete(id?: number) {
    const ids = [id || removeIds.value].join(",");
    if (!ids) {
      ElMessage.warning("请勾选删除项");
      return;
    }

    ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(
        () => {
          loading.value = true;
                BigcapAPI.deleteByIds(ids)
              .then(() => {
                ElMessage.success("删除成功");
                handleResetQuery();
              })
              .finally(() => (loading.value = false));
        },
        () => {
          ElMessage.info("已取消删除");
        }
    );
  }

  onMounted(() => {
    handleQuery();
  });
</script>
